//
//  LGOutpatientRecordDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/3.
//

#import "LGOutRecordDetailController.h"
#import "LGReportDetailPhotoController.h"
#import "LGReportDetailClinicController.h"
#import "LGReportDetailPCIController.h"
#import "LGReportDetailHospitalController.h"
#import "LGReportDetailHospitalController2.h"

#import "LGReportDetailItemCell.h"

#define viewHeight  (Screen_H-topBarHeight-viewPix(120))

@interface LGOutRecordDetailController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)UIPageControl *pageControl;
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGOutRecordDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"手术相关报告";
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.pageControl];
    [self.view addSubview:self.scrollView];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight)];
    self.emptyView.backgroundColor = [UIColor clearColor];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Visitrecords/visitRecordSummary" parameters:@{@"patient_id":self.patientid,@"report_date":self.reportDate} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无手术相关报告" offset:0];
        }
    }];
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    if(dataArray.count>0){
        NSInteger index = 0;
        for (NSInteger i=0; i<dataArray.count; i++) {
            NSDictionary *tempDic = dataArray[i];
            NSString *source = LGNSString(tempDic[@"source"]);
            NSString *type = LGNSString(tempDic[@"type"]);
            NSString *version = LGNSString(tempDic[@"version"]);
            NSString *recordid = LGNSString(tempDic[@"content"][@"record_id"]);
            index = [recordid isEqualToString:self.recordid]?i:index;

            if([source isEqualToString:@"corrected"]){
                //识别的
                if([type isEqualToString:@"210"]){
                    //门诊病历
                    LGReportDetailClinicController *clinicVC = [[LGReportDetailClinicController alloc]init];
                    clinicVC.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
                    clinicVC.dataDic = tempDic[@"content"];
                    [self addChildViewController:clinicVC];
                    [self.scrollView addSubview:clinicVC.view];
                    
                }else if([type isEqualToString:@"28"]){
                    //出院病情证明书
                    if([version isEqualToString:@"1"]){
                        LGReportDetailHospitalController *hospitalVC = [[LGReportDetailHospitalController alloc]init];
                        hospitalVC.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
                        hospitalVC.dataDic = tempDic[@"content"];
                        [self addChildViewController:hospitalVC];
                        [self.scrollView addSubview:hospitalVC.view];
                        
                    }else{
                        LGReportDetailHospitalController2 *hospitalVC2 = [[LGReportDetailHospitalController2 alloc]init];
                        hospitalVC2.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
                        hospitalVC2.dataDic = tempDic[@"content"];
                        [self addChildViewController:hospitalVC2];
                        [self.scrollView addSubview:hospitalVC2.view];
                    }
                }else{
                    //501、502、503  三种报告
                    LGReportDetailPCIController *pciVC = [[LGReportDetailPCIController alloc]init];
                    pciVC.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
                    pciVC.type = type;
                    pciVC.dataDic = tempDic[@"content"];
                    [self addChildViewController:pciVC];
                    [self.scrollView addSubview:pciVC.view];
                }
            }else{
                //医生、患者自己上传的
                LGReportDetailPhotoController *photoVC = [[LGReportDetailPhotoController alloc]init];
                photoVC.view.frame = CGRectMake(Screen_W*i, 0, Screen_W, viewHeight);
                photoVC.dataDic = tempDic[@"content"];
                [self addChildViewController:photoVC];
                [self.scrollView addSubview:photoVC.view];
            }
        }
        self.scrollView.contentSize = CGSizeMake(Screen_W*dataArray.count, viewHeight);
        [self.collectionView reloadData];
        self.pageControl.numberOfPages = dataArray.count;
        self.pageControl.currentPage = index;
        [self.scrollView setContentOffset:CGPointMake(Screen_W*index, 0) animated:NO];
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        
    }else{
        [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无手术相关报告" offset:0];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGReportDetailItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.dataDic = self.dataArray[indexPath.item];
    return cell;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if(scrollView == self.collectionView){
        NSInteger index = self.collectionView.contentOffset.x/viewPix(353);
        [self.scrollView setContentOffset:CGPointMake(Screen_W*index, 0) animated:YES];
        [self.pageControl setCurrentPage:index];
    }else{
        NSInteger index = self.scrollView.contentOffset.x/Screen_W;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        [self.pageControl setCurrentPage:index];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(viewPix(353), viewPix(86));
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake((Screen_W-viewPix(353))/2.0, viewPix(10)+topBarHeight, viewPix(353), viewPix(86)) collectionViewLayout:layout];
        [_collectionView registerClass:[LGReportDetailItemCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.pagingEnabled = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.cornerRidus = 15;
    }
    return _collectionView;
}

-(UIPageControl *)pageControl{
    if(!_pageControl){
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(104), Screen_W, viewPix(15))];
        _pageControl.pageIndicatorTintColor = [UIColor colorWithString:@"#CBDCFB"];
        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithString:@"#4C87FD"];
    }
    return _pageControl;
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(120), Screen_W, Screen_H-topBarHeight-viewPix(120))];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

@end
